package com.yyx.server.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.yyx.server.pojo.Admin;
import com.yyx.server.pojo.Role;
import com.yyx.server.service.AdminService;
import com.yyx.server.service.RoleService;
import com.yyx.server.vo.ResultVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YongXin
 * @since 2021-07-24
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {


    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;

    @ApiOperation("获取所有或模糊搜索操作员")
    @GetMapping("/")
    public List<Admin> getAllAdmins(String keywords){
        System.out.println(keywords);
        return adminService.getAllAdmins(keywords);
    }
    @ApiOperation("更新操作员")
    @PutMapping("/")
    public ResultVo updAdmin(@RequestBody Admin admin){
        System.out.println(admin);
        return adminService.updateAdmin(admin);
    }
    @ApiOperation("更新管理员状态")
    @PutMapping("/status/{id}/{enabled}")
    public ResultVo updAdminStatus(@PathVariable Integer id,@PathVariable String enabled){
        System.out.println(enabled);
        boolean update = adminService.update(new UpdateWrapper<Admin>().set("enabled", enabled).eq("id", id));
        if (update){
            return ResultVo.success("更新成功");
        }
        return ResultVo.error("更新失败");
    }
    @ApiOperation("删除操作员")
    @DeleteMapping("/{id}")
    public ResultVo delAdmin(@PathVariable Integer id){
        if (adminService.removeById(id)){
            return ResultVo.success("删除成功");
        }
        return ResultVo.error("删除失败");
    }
    @ApiOperation("获取所有角色")
    @GetMapping("/roles")
    public List<Role> getAllRoles(){
        return roleService.list();
    }
    @ApiOperation("更新操作员角色")
    @PutMapping("/roles")
    public ResultVo updAdminRoles(Integer aid,Integer[] rids){
       return adminService.updAdminRoles(aid,rids);
    }
}

